package com.feng.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.commonutils.JwtUtils;
import com.feng.commonutils.R;
import com.feng.commonutils.vo.UcenterMemberVo;
import com.feng.eduservice.client.UcenterClient;
import com.feng.eduservice.entity.EduComment;
import com.feng.eduservice.mapper.EduCommentMapper;
import com.feng.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.servicebase.excecption.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-29
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {
    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public R getCommentPage(Long page, Long limit, String courseId) {
        Page<EduComment> commentPage=new Page<>(page,limit);
        LambdaQueryWrapper<EduComment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduComment::getCourseId,courseId);
        //按最新排序
        queryWrapper.orderByDesc(EduComment::getGmtCreate);
        baseMapper.selectPage(commentPage,queryWrapper);
        List<EduComment> commentList = commentPage.getRecords();
        long current = commentPage.getCurrent();//当前页
        long size = commentPage.getSize();//一页记录数
        long total = commentPage.getTotal();//总记录数
        long pages = commentPage.getPages();//总页数
        boolean hasPrevious = commentPage.hasPrevious();//是否有上页
        boolean hasNext = commentPage.hasNext();//是否有下页
        HashMap<String, Object> map = new HashMap<>();
        map.put("current",current);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("list",commentList);

       return R.ok().data(map);

    }

    @Override
    public R addComment(HttpServletRequest request, EduComment eduComment) {
        //登陆后的用户，会将token传至前端，前端会将token存至cookie，同时拦截器会将cookie放入request的头部
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //看memberId是否能通过token获取到值
        if(StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"请先登录");
        }
        //到这里证明已经登录
        //设置用户id
        eduComment.setMemberId(memberId);
        //同时需要将个人信息获取到返回给前端进行展示
        UcenterMemberVo memberVo = ucenterClient.GetMemberInfoById(memberId);
        eduComment.setAvatar(memberVo.getAvatar());
        eduComment.setNickname(memberVo.getNickname());
        this.save(eduComment);
        return R.ok();
    }
}
